//
//  NewFeatureViewController.swift
//  MQWeibo
//
//  Created by SYN on 15/11/20.
//  Copyright © 2015年 SYN. All rights reserved.
//

import UIKit

private let reuseIdentifier = "NFCell"

class NewFeatureViewController: UICollectionViewController {
    
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    // 提示: 在不同的控制器中,具体设置布局属性前,一定记住几个生命周期函数
    // 确定选择一个最合适的函数
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        print("\(__FUNCTION__) --> \(view.frame)")
        
        layout.itemSize = view.bounds.size
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //图片总数
    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    // cell 显示完成调用
    // 注意: 参数:cell&indexPath都是之前消失那个 cell 的
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //获取正在显示的 cell
        let path = collectionView.indexPathsForVisibleItems().last! as NSIndexPath
        
        if path.item == imageCount - 1 {
            // 根据 indexPath 或得到 cell
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            // 播放动画
            cell.showStartButton()
            
        }
        
    }
    
    
}

class NewFeatureCell: UICollectionViewCell {
    
    @IBOutlet weak var iconImage: UIImageView!
    
    @IBOutlet weak var startButton: UIButton!
    
    var imageIndex: Int = 0 {
        
        didSet{
            //设置图像
            iconImage.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            //隐藏启动按钮
            startButton.hidden = true
            
        }
        
    }
    
    //显示启动按钮
    func showStartButton() {
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //        UIView.animateWithDuration(1.2, animations: { () -> Void in
        //            self.startButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
        //            }) { (_) -> Void in
        //                print("ok")
        //        }
        
        // 上面那个动画方法已经过时了
        // usingSpringWithDamping : 弹力系数 0.0~1.0 数值越小 弹力越大
        // initialSpringVelocity : 初始速度 开始越快 有可能回弹
        // options : 如果不需要指定 可以设置为 nil
        // JSON 序列化 位移的枚举 如果不想做参数 OC 可以使用 0 效率最快 swift 用 nil
        // UIViewAnimationOptions.BeginFromCurrentState : 从当前开始
        // nil -> 对象 & NULL -> 地址
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5.0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
            self.startButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
            }) { (_) -> Void in
                print("OK")
        }
        
        
    }
    
    @IBAction func clickButton() {
        
        print(__FUNCTION__)
    }
    
}
